/**
 * Created by foredawn on 15-8-19.
 * mongodb connection module
 */

var mongoose = require('mongoose');
var url;
var noAuthUrl;
exports.config = function (conf) {
    if (!conf) {
        conf = {
            host: "localhost",
            port: "27017",
            db: "default"
        };
        console.info('connected default db.')
    }
    if (!conf.host) {
        conf.host = "localhost";
    }
    if (!conf.port) {
        conf.port = "27017";
    }
    var auth = conf.auth;
    url = 'mongodb://' + (auth ? auth.username + ":" + auth.password + "@" : "") + conf.host + ':' + conf.port + '/' + conf.db;
    noAuthUrl = 'mongodb://' + conf.host + ':' + conf.port + '/' + conf.db;
}


//连接数据库
exports.connection = function () {
    mongoose.connect(url, function (err) {
        if (!err) {
            console.info('connection success');
        } else {
            console.info('connection error:' + err.message);
        }
    });
}